package cn.usian.controller;

import cn.usian.common.R;
import cn.usian.pojo.Dish;
import cn.usian.pojo.DishDto;
import cn.usian.service.DishService;
import cn.usian.service.SetmealDishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealDishService setmealDishService;

    //分页
    @GetMapping("/page")
    public R findPage(@RequestParam(defaultValue = "1")int page, @RequestParam(defaultValue = "10")int pageSize, String name){
       return dishService.findPage(page,pageSize,name);
    }

    //添加
    @PostMapping
    public R save(@RequestBody DishDto dishDto, HttpSession session){
        dishDto.setStatus(1);//1.代表菜品启用 0.代表下架商品
        dishDto.setSort(0);
        dishDto.setCreateTime(LocalDateTime.now());
        dishDto.setUpdateTime(LocalDateTime.now());
        //1.补全创建人与修改
        session.getAttribute("employee");
        Long id =(Long) session.getAttribute("employee");
        dishDto.setCreateUser(id);
        dishDto.setUpdateUser(id);

        //2.把数据交给service
        dishService.save(dishDto);
        return R.success("添加成功");
    }

    //会显
    @GetMapping("{id}")
    public R findById(@PathVariable Long id){
        return dishService.findById(id);
    }
    //修改

    @PutMapping
    public R update(@RequestBody DishDto dishDto, HttpSession session){

        //1.补全创建人与修改
        session.getAttribute("employee");
        Long id =(Long) session.getAttribute("employee");
        dishDto.setUpdateUser(id);
        dishDto.setCreateUser(id);
        //2.把数据交给service
        dishService.updateDish(dishDto);
        return R.success("添加成功");
    }
    //批量删除
    @DeleteMapping
    public R del(Long[] ids){
        dishService.del(ids);
        return R.success("删除成功");
    }
    //批量启动
    @PostMapping("status/{status}")
    public R status(@PathVariable("status")Integer status, Long[] ids, HttpSession session){
        DishDto dishDto = new DishDto();
        Long id =(Long) session.getAttribute("employee");
        dishDto.setCreateTime(LocalDateTime.now());
        dishDto.setUpdateTime(LocalDateTime.now());
        dishDto.setUpdateUser(id);
        dishDto.setCreateUser(id);
        return dishService.status(status,ids);
    }

    @GetMapping("list")
    public R findCategory(Dish dish){
        return setmealDishService.findCategory(dish);
    }
}
